//
//  HNSH_ExchangeYouKaViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/4/4.
//

#import "HNSH_ExchangeYouKaViewController.h"

#import "HNSH_ExchangeHuaFeiItem.h"

#import "ST_PayPasswordView.h"           //支付弹窗

#import "HNSH_ExchangeNetServer.h"

#import "HNSH_CashierPayViewController.h"

@interface HNSH_ExchangeYouKaViewController ()

/* scrollView */
@property (nonatomic, strong) UIScrollView *scrollView;
/* phoneField */
@property (nonatomic, strong) UITextField *phoneField;

/* desString */
@property (nonatomic, copy) NSString *desString;

/* itemAry */
@property (nonatomic, strong) NSMutableArray *itemAry;

/* currentModel */
@property (nonatomic, strong) HNSH_ExchangeYouFeiItemModel *currentModel;


@end

@implementation HNSH_ExchangeYouKaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavTitle:@"油卡充值" showLeftItem:YES];
    
    self.view.backgroundColor = [UIColor appWhiteLabelColor];
    
    [self loadHomeScrollData];
    
    [self.view addSubview:self.scrollView];
    
}

- (void)configTopViewWithMoneyItems:(NSArray *)moneyItems {
    [self.scrollView addSubview:self.phoneField];
    
    UIView *lineView = [[UIView alloc]initWithFrame:(CGRect)CGRectMake(16, 68, kScreenWidth-32, 1)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
    [self.scrollView addSubview:lineView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 96, 200, 22)];
    titleLabel.text = @"充值金额";
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    titleLabel.textColor = [UIColor appBlack333LabelColor];
    [self.scrollView addSubview:titleLabel];
    
    NSInteger itemCount = moneyItems.count;
    
    CGFloat width = (kScreenWidth-16)/3;
    CGFloat height = 96;
    self.itemAry = [NSMutableArray arrayWithCapacity:itemCount];
    __weak __typeof(&*self)weakSelf = self;
    for (int i = 0; i < itemCount; i ++) {
        HNSH_ExchangeHuaFeiItem *item = [[HNSH_ExchangeHuaFeiItem alloc]initWithFrame:CGRectMake(8+width*(i%3), 128+(height+10)*(i/3), width, height)];
        item.select = i == 0;
        if (i == 0) {
            self.currentModel = moneyItems[i];
        }
        item.ykModel = moneyItems[i];
        [self.scrollView addSubview:item];
        [self.itemAry addObject:item];
        
        item.ykClickBlock = ^(HNSH_ExchangeYouFeiItemModel * _Nonnull selectModel) {
            for (HNSH_ExchangeHuaFeiItem *itemView in weakSelf.itemAry) {
                itemView.select = NO;
            }
            weakSelf.currentModel = selectModel;
        };
    }
    
    NSInteger row = itemCount%3 > 0 ? 1 : 0;
    CGFloat desY = 128+(height+10)*(itemCount/3 + row) + 18;
    
    UILabel *desLabel = [[UILabel alloc]initWithFrame:CGRectMake(16, desY, kScreenWidth-32, 22)];
    desLabel.text = self.desString;
    desLabel.numberOfLines = 0;
    desLabel.attributedText = [self getHtmlStringWithString:self.desString];
    desLabel.font = [UIFont systemFontOfSize:13];
    desLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    [self.scrollView addSubview:desLabel];
    [desLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(desY);
        make.left.mas_equalTo(16);
        make.width.mas_equalTo(kScreenWidth-32);
    }];
    
    self.scrollView.contentSize = CGSizeMake(0, 1000);
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"立即充值" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    button.backgroundColor = [UIColor appGreenColor];
    [self.view addSubview:button];
    button.layer.cornerRadius = 25.0f;
    button.layer.masksToBounds = YES;
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.height.mas_equalTo(50);
        make.right.mas_equalTo(-30);
        make.bottom.mas_equalTo(-6-kBottomSafeHeight);
    }];
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf loadPayMoney];
    }];
}

#pragma mark networking
- (void)loadHomeScrollData {
    [HNSH_ExchangeNetServer requestExchangeProductYouKaConfigSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            NSArray *moneyItems = [NSArray yy_modelArrayWithClass:[HNSH_ExchangeYouFeiItemModel class] json:responseObject.resultset[@"spec_list"]];
            self.desString = responseObject.resultset[@"xuzhi"];
            [self configTopViewWithMoneyItems:moneyItems];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)loadPayMoney {
    NSDictionary *dic = @{@"spec_id":self.currentModel.spec_id,
                          @"account":self.phoneField.text,
                          @"type":@"2"
    };
    [HNSH_ExchangeNetServer requestExchangeProductSubmitWithInfo:dic success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            HNSH_CashierPayModel * model = [HNSH_CashierPayModel yy_modelWithDictionary:responseObject.resultset];
            [self pushVCWithModel:model];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)pushVCWithModel:(HNSH_CashierPayModel *)model{
    HNSH_CashierPayViewController * vc = [HNSH_CashierPayViewController new];
    vc.payModel = model;
    vc.hiddenOrder = YES;
    [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
}

- (NSMutableAttributedString *)getHtmlStringWithString:(NSString *)string {
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                              NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding)};
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
    // 设置段落格式
    NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc] init];
    para.lineSpacing = 8;
    [attStr addAttribute:NSParagraphStyleAttributeName value:para range:NSMakeRange(0, attStr.length)];
    // 设置文本的Font
    [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, attStr.length)];
    [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor appGrayLabelColor] range:NSMakeRange(0, attStr.length)];
    return attStr;
}

#pragma mark lazy
- (UITextField *)phoneField {
    if (!_phoneField) {
        _phoneField = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, kScreenWidth-40, 40)];
        _phoneField.textColor = [UIColor appBlack333LabelColor];
        _phoneField.font = [UIFont boldSystemFontOfSize:28];
        _phoneField.placeholder = @"请输入手机号";
        _phoneField.keyboardType = UIKeyboardTypePhonePad;
//        _phoneField.userInteractionEnabled = NO;
//        _phoneField.text = [ST_UserManager shareInstance].user.phone;
    }
    return _phoneField;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight-kNavBarAndStatusBarHeight-62-kBottomSafeHeight)];
        _scrollView.contentSize = CGSizeMake(0, 900);
        _scrollView.backgroundColor = [UIColor appWhiteLabelColor];
    }
    return _scrollView;
}
@end
